package com.dycx.cms.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dycx.cms.converter.AdConverter;
import com.dycx.cms.dto.AdDTO;
import com.dycx.cms.entity.AdDO;
import com.dycx.cms.entity.AdTypeDO;
import com.dycx.cms.mapper.AdMapper;
import com.dycx.cms.service.AdService;
import com.dycx.cms.service.AdTypeService;
import com.dycx.cms.vo.AdVO;
import com.dycx.system.service.OssFileService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
@AllArgsConstructor
public class AdImpl extends ServiceImpl<AdMapper, AdDO> implements AdService {

    private AdMapper adMapper;
    private OssFileService ossFileService;
    private AdTypeService adTypeService;

    @Override
    public Boolean delete(int id) {
        AdDO oldAdEntity = this.getById(id);

        if (!this.removeById(id)) return false;

        ossFileService.setDeleted(oldAdEntity.getImage());
        ossFileService.setDeleted(oldAdEntity.getImage(), true);

        return true;
    }

    /**
     * 添加
     *
     * @param adDto
     * @return
     */
    @Override
    public Boolean add(AdDTO adDto) {
        AdDO adEntity = AdConverter.dtoToEntity(adDto);
        if (!this.save(adEntity)) return false;
        ossFileService.setUse(adDto.getImage());
        adDto.setId(adEntity.getId());
        return true;
    }

    /**
     * 添加/修改
     *
     * @param adDto
     * @return
     */
    @Override
    public Boolean edit(AdDTO adDto) {
        AdDO oldAdEntity = this.getById(adDto.getId());

        AdDO adEntity = AdConverter.dtoToEntity(adDto);
        if (!this.updateById(adEntity)) return false;

        ossFileService.setDeleted(oldAdEntity.getImage());
        ossFileService.setUse(adEntity.getImage());

        return true;
    }

    /**
     * 分页
     *
     * @return
     */
    @Override
    public Page<AdVO> getPage(AdDTO dto) {
        LambdaQueryWrapper<AdDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StrUtil.isNotBlank(dto.getTitle()), AdDO::getTitle, dto.getTitle());
        wrapper.eq(ObjectUtil.isNotNull(dto.getAdTypeId()), AdDO::getAdTypeId, dto.getAdTypeId());

        if (StrUtil.isBlank(dto.getOrderBy())) {
            wrapper.orderByDesc(AdDO::getId);
        } else {
            wrapper.last(" order by " + dto.getOrderBy());
        }

        Page<AdDO> entityPage = page(new Page<>(dto.getPageIndex(), dto.getPageSize()), wrapper);
        Page<AdVO> voPage = AdConverter.entityToVoPage(entityPage);

        //查询分类名
        List<Integer> ids = voPage.getRecords().stream().map(AdVO::getId).collect(Collectors.toList());
        LambdaQueryWrapper<AdTypeDO> typeWrapper = new LambdaQueryWrapper<>();
        typeWrapper.in(AdTypeDO::getId, ids);
        List<AdTypeDO> adTypeDOList = adTypeService.list(typeWrapper);
        voPage.getRecords().forEach(item->{
            List<AdTypeDO> filterList = adTypeDOList.stream().filter(o->o.getId().equals(item.getAdTypeId())).collect(Collectors.toList());
            if(ObjectUtil.isNotEmpty(filterList) && filterList.size() > 0){
                item.setAdTypeName(filterList.get(0).getAdTypeName());
            }
        });

        return voPage;
    }

    /**
     * 更新排序
     *
     * @param adDto
     * @return
     */
    @Override
    public Boolean updateSort(AdDTO adDto) {
        AdDO ad = new AdDO();
        ad.setId(adDto.getId());
        ad.setSort(adDto.getSort());
        return this.updateById(ad);
    }

    /**
     * 更新状态
     *
     * @param adDto
     * @return
     */
    @Override
    public Boolean updateStatus(AdDTO adDto) {
        AdDO ad = new AdDO();
        ad.setId(adDto.getId());
        ad.setStatus(adDto.getStatus());
        return this.updateById(ad);
    }

    /**
     * 获取所有列表
     *
     * @return
     */
    @Override
    public List<AdVO> getAllList(Integer adTypeId) {
        LambdaQueryWrapper<AdDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AdDO::getStatus, true);
        queryWrapper.eq(AdDO::getAdTypeId, adTypeId);
        queryWrapper.orderByDesc(AdDO::getSort);

        return AdConverter.entityToVoList(this.list(queryWrapper));
    }

}
